package com.toe.forum.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.StateEnum;
import com.toe.pojo.dto.forum.AlbumAddDTO;
import com.toe.pojo.dto.forum.AlbumUpdateDTO;
import com.toe.pojo.entity.forum.ForumAlbum;
import com.toe.forum.webapi.mapper.ForumAlbumMapper;
import com.toe.forum.service.ForumAlbumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.pojo.vo.forum.AlbumVO;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 相册 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@Slf4j
public class ForumAlbumServiceImpl extends ServiceImpl<ForumAlbumMapper, ForumAlbum> implements ForumAlbumService {

    @Autowired
    private ForumAlbumMapper forumAlbumMapper;

    @Override
    public void insert(AlbumAddDTO albumAddDTO) {
        log.info("开始执行添加图片到相册的业务!!!!");
//        long articleId = albumAddDTO.getArticleId();
//        String url = albumAddDTO.getUrl();
        //实际业务中,起始无需添加,业务UUID生成的id值绝对是唯一的,也就是说添加图片不会重复
//        log.info("验证当前图片是否已经存在!");
//        AlbumVO albumVO = forumAlbumMapper.selectAlbum(articleId, url);
//        if (albumVO!=null){
//            log.info("图片已经存在,无法重复添加");
//            throw new ToeServiceException(StateEnum.ERR_CLIENT,"图片已经存在,无法重复添加");
//        }
        LocalDateTime now = LocalDateTime.now();
        ForumAlbum forumAlbum = new ForumAlbum();
        forumAlbum.setArticleId(albumAddDTO.getArticleId());
        forumAlbum.setUrl(albumAddDTO.getUrl());
        forumAlbum.setCreateTime(now);
        forumAlbum.setUpdateTime(now);
        forumAlbumMapper.insert(forumAlbum);
    }

    @Override
    public void deleteById(long id) {
        //此处也无需验证数据是否存在,如果数据不存在,受影响行数也为0
        forumAlbumMapper.deleteById(id);
    }

    @Override
    public void updateAlbum(AlbumUpdateDTO albumUpdateDTO) {
//        //同理这个验证逻辑也是几乎不会用到,可以注释
//        log.info("验证当前图片是否已经存在!");
//        long articleId = albumUpdateDTO.getArticleId();
//        String url = albumUpdateDTO.getUrl();
//        AlbumVO albumVO = forumAlbumMapper.selectAlbum(articleId,url);
//        if (albumVO!=null){
//            log.info("图片已经存在,请换一张图片!");
//            throw new ToeServiceException(StateEnum.ERR_CLIENT,"图片已经存在,请换一张图片!");
//        }
        forumAlbumMapper.updateAlbumByID(albumUpdateDTO);
    }

    @Override
    public List<AlbumVO> selectAllAlbum() {
        log.info("开始执行查询全部图片业务!");
        List<AlbumVO> albumVOS = forumAlbumMapper.selectAllAlbum();
        return albumVOS;
    }

    @Override
    public JsonPage<AlbumVO> selectAllAlbumPage(Integer pageNum, Integer pageSize) {
        log.info("开始执行分页查询全部图片业务!");
        PageHelper.startPage(pageNum,pageSize);
        List<AlbumVO> albumVOS = forumAlbumMapper.selectAllAlbum();
        return JsonPage.restPage(new PageInfo<>(albumVOS));
    }

    @Override
    public List<AlbumVO> selectAllAlbumByArticleId(long articleId) {
        log.info("开始执行查询某个文章全部图片业务!");
        List<AlbumVO> albumVOS = forumAlbumMapper.selectAllAlbumByArticleId(articleId);
        return albumVOS;
    }

    @Override
    public JsonPage<AlbumVO> selectAllAlbumByArticleIdPage(long articleId, Integer pageNum, Integer pageSize) {
        log.info("开始执行分页查询某个文章全部图片业务!");
        PageHelper.startPage(pageNum,pageSize);
        List<AlbumVO> albumVOS = forumAlbumMapper.selectAllAlbumByArticleId(articleId);
        return JsonPage.restPage(new PageInfo<>(albumVOS));
    }

    @Override
    public AlbumVO selectAlbumById(long id) {
        log.info("开始执行查询某张图片的业务!");
        AlbumVO albumVO = forumAlbumMapper.selectAlbumById(id);
        return albumVO;
    }
}
